package main

import (
	"net/http"
	"strconv" 
	"time"

	"github.com/gin-gonic/gin"
)

// 提醒结构体
type Reminder struct {
	ID          int       `json:"id"`
	CreatorID   int       `json:"creator_id"`
	Content     string    `json:"content"`
	ReminderTime time.Time `json:"reminder_time"`
}

// 内存中的提醒列表
var reminders []Reminder
var nextID = 1

// 增加提醒
func addReminder(c *gin.Context) {
	var newReminder Reminder
	if err := c.BindJSON(&newReminder); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	newReminder.ID = nextID
	nextID++
	reminders = append(reminders, newReminder)
	c.JSON(http.StatusCreated, newReminder)
}

// 获取特定用户的提醒列表
func getReminders(c *gin.Context) {
	creatorID := c.Param("creator_id")
	var userReminders []Reminder
	for _, reminder := range reminders {
		if creatorID == strconv.Itoa(reminder.CreatorID) { // 使用 strconv
			userReminders = append(userReminders, reminder)
		}
	}
	c.JSON(http.StatusOK, userReminders)
}

// 删除提醒
func deleteReminder(c *gin.Context) {
	reminderID := c.Param("id")
	for i, reminder := range reminders {
		if reminderID == strconv.Itoa(reminder.ID) { // 使用 strconv
			reminders = append(reminders[:i], reminders[i+1:]...)
			c.JSON(http.StatusOK, gin.H{"message": "Reminder deleted"})
			return
		}
	}
	c.JSON(http.StatusNotFound, gin.H{"error": "Reminder not found"})
}

// 更新提醒时间或内容
func updateReminder(c *gin.Context) {
	reminderID := c.Param("id")
	var updatedReminder Reminder
	if err := c.BindJSON(&updatedReminder); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	for i, reminder := range reminders {
		if reminderID == strconv.Itoa(reminder.ID) { // 使用 strconv
			reminders[i].Content = updatedReminder.Content
			reminders[i].ReminderTime = updatedReminder.ReminderTime
			c.JSON(http.StatusOK, reminders[i])
			return
		}
	}
	c.JSON(http.StatusNotFound, gin.H{"error": "Reminder not found"})
}

func main() {
	r := gin.Default()

	// 创建提醒
	r.POST("/reminders", addReminder)

	// 获取某个用户的提醒列表
	r.GET("/reminders/:creator_id", getReminders)

	// 删除提醒
	r.DELETE("/reminders/:id", deleteReminder)

	// 更新提醒
	r.PUT("/reminders/:id", updateReminder)

	r.Run(":8080") // 在8080端口运行服务
}
